package com.yc.txx.servlet;

import com.google.gson.Gson;
import com.yc.txx.biz.BizExcpetion;
import com.yc.txx.biz.UserBiz;
import com.yc.txx.entity.User;
import com.yc.txx.vo.Result;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "LoginServlet", value = "/LoginServlet.s")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        final String name = request.getParameter("name");
        final String pwd = request.getParameter("pwd");
        UserBiz biz = new UserBiz();
        Result result;
        try {
            final User user = biz.login(name, pwd);
            request.getSession().setAttribute("myinfo",user);
            result = Result.success("登录成功!");
        } catch (BizExcpetion bizExcpetion) {
            bizExcpetion.printStackTrace();
            result = Result.failure(bizExcpetion.getMessage());
        }
        final String json = new Gson().toJson(result);
        response.getWriter().append(json);
    }
}
